home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <dos.h>
- #include <memory.h>
- #include <process.h>
- #include <conio.h>
- #include <graph.h>
- #define CONTROL 0x34
- #define CONT2 0xb4
- #define COMLINE 62 /* (f4) */
- #define THRESH 63 /* (f5) */
- #define UP 72
- #define DOWN 80
- #define PGUP 73
- #define PGDOWN 81
- #define RIGHT 77
- #define LEFT 75
- #define CNRIGHT 116
- #define CNLEFT 115
- #define HOME 71
- #define END 79
- #define CNHOME 119
- #define CNEND 117
- #define CLOSE 61 /* (f3) */
- #define TCONSTU 60 /* (f2) */
- #define TCONSTD 59 /* (f1) */
- #define TCONST_NEW 68 /* (f10) */
- #define GO inp(0x306)
- #define STOP inp(0x307)
- unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512];
- int spect_count,y[512],zh[512],threshold;
- long z[512];
- float norm[512],trate,freqmax;
- char *adr1, *adr2;
- cdecl sigpts();
- cdecl main(argc,argv)
- int argc;
- char *argv[];
- {
- char c,scr_csts();
- int cmin,j,jh,k,i,scale,offset,kount;
- int ct,binmax;
- long count,jjj,jj,jjmax,jjmore,jjcnt;
- float zhold[513],zsum[512],tconst,rate,osc,amplitude;
-
- seg320=0xd000;
- adr1 = (char *)0xD0001800L;
- adr2 = (char *)0xD0001C00L;
- tconst=0.5;
- jjcnt=0;
- io320=0x300; /* base i/o address for 320 board */
- osc = 25e6/4.; /* basic board oscillator */
- cntrlpt = io320 + 7;
- prt0 = io320 + 4;
- prt2 = io320 + 6;
- scale = 5;
- jjmore = 0;
- offset = 180;
- clark: do {
- cls();
- printf("Input desired sample rate in Hz ");
- scanf( "%f",&rate);
- } while ( (rate < 80.0 ) || (rate > 40000.1));
- /* count gives you the oscillator "divisor" just as in SIO's */
- count = osc/rate;
- cmin = osc /40000.0;
- if ( count < 2 ) count = 2;
- trate = osc /(float)count;
- printf("\nRate = %f ",trate);
- /* set timers for desired sampling rate */
- control_timer(count,osc,&trate);
- _settextposition(24,1);
- printf("%7.2f Hz ",trate);
- for(k=0;k<20000;k++) { /* time delay for 8254 & clear holding arrays */
-
- if (k<513) {
- zhold[k]=0.;
- if (k<512) norm[k]=0.;
- }
- }
-
- STOP; /* halt */
- /* Load the fft and square routines */
- spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
- /* Run it once to set up arrays and have proper values in the ports */
- GO; /* go */
- while(inp(io320)!=88) {};
- STOP; /* stop */
- /* Generate Reference Baseline Spectrum */
- printf("\n How many samples in reference spectrum? ");
- scanf("%d",&spect_count);
- if (spect_count == 0) {
- for(k=0;k<512;k++) norm[k]=0.05;
- } else ref_spect(spect_count);
- STOP;
- printf("\nReference spectrum --- Hit any character when ready");
- ci();
- _setvideomode(_TEXTC80);
- _settextposition(1,1);
- printf("\n How many samples for each displayed spectrum? ");
- scanf("%d",&spect_count);
-
- /* Set it up to run forever until ^C or F3 to change screen and quit*/
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- kount=0;
- GO;
- while (1)
- {
- if ((c = scr_csts()) != 0 )
- {
- switch(c) {
- case COMLINE: {
- _setvideomode(_TEXTC80);;
- goto clark;
- break;
- }
- case THRESH: {
- _setvideomode(_TEXTC80);;
- printf("Input threshold %d? ",threshold);
- scanf("%d",&threshold);
- if (threshold < 1) threshold=0;
- if (threshold > 199) threshold = 199;
- if (threshold==0) outp(97,inp(97)&0xFC);
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- break;
- }
- case UP: {
- scale -= 1;
- if (scale < 0) scale = 0;
- break;
- }
- case DOWN: {
- scale += 1;
- if (scale > 15) scale = 15;
- break;
- }
- case PGUP: {
- offset -= 10;
- if (offset < 0) offset = 0;
- break;
- }
- case PGDOWN: {
- offset += 10;
- if (offset > 180) offset = 180;
- break;
- }
- case CLOSE: {
- STOP; /* halt */
- outp(97,inp(97)&0xFC);
- _setvideomode(_TEXTC80);;
- exit();
- break;
- }
- case HOME: {
- jjmore -= 1;
- if (jjmore < 0) jjmore = 0;
- break;
- }
- case END: {
- jjmore += 1;
- if (jjmore > 10000) jjmore = 10000;
- break;
- }
- case CNHOME: {
- jjmore /= 2;
- if (jjmore < 0) jjmore = 0;
- break;
- }
- case CNEND: {
- jjmore *= 2;
- if (jjmore > 10000) jjmore = 10000;
- break;
- }
- case TCONSTU: {
- tconst *= 2.0;
- if (tconst>1.0) tconst=1.0;
- break;
- }
- case TCONST_NEW: {
- _setvideomode(_TEXTC80);; printf("%3d\n",c);
- printf(" tconst currently = %9.7f --- Enter new value: ",tconst);
- scanf("%f",&tconst); cls();
- if (tconst> 1.0) tconst= 1.0;
- if (tconst<.0001) tconst=.0001 ;
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- break;
- }
- case TCONSTD: {
- tconst *= 0.5;
- if (tconst<.0001) tconst=.0001 ;
- break;
- }
- default:
- break;
- }
- }
-
- if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<6)) /* is there data? */
- { /* bypass the following loop if there isn't */
- for (k=0;k<512;k++) {
- if (kount == 0) {
- zsum[k]=z[k];
- }
- else zsum[k] += z[k];
- }
- kount++;
- if (kount >= spect_count) {
- kount=0;
- for (k=0;k<512;k++) zsum[k] *= norm[k];
- amplitude=0.;
- binmax=128;
- for(k=1;k<511;k++) {
- zhold[k]=(1.0-tconst)*zhold[k] +
- (zsum[k]*2.0+zsum[k-1]+zsum[k+1])*0.25*tconst;
- y[k]= offset-(((int)zhold[k])>>scale);
- zsum[k] = 0;
- if (y[k] < 1) y[k]=1; /* don't go off the top */
- if (k > 80 && zhold[k]>amplitude) { /* avoid DC peak */
- binmax=k;
- amplitude=zhold[k];
- }
- }
- _settextposition(24,1);
- freqmax=((float)binmax)*trate/1024.0;
- printf("Rate= %5.0f Hz Scale= %d tconst= % 5.4f sampl= %d freq=% 5.0f amp=% 4.0f ",
- trate,scale,tconst,spect_count,freqmax,amplitude);
- sigpts(&y[0],512); /* show old fft */
- if (threshold!=0){
- if (y[binmax]<threshold) {
- outp(97,inp(97)&0xFC);
- prt1=1193180.0/freqmax;
- outp(67,182);
- outp(66,prt1&0xFF);
- outp(66,(prt1>>8)&0xFF);
- outp(97,inp(97)|3);
- }
- else outp(97,inp(97)&0xFC);
- }
-
- }
- } /* end of bypass loop -- display counter even if no data at input */
-
- _settextposition(1,1); printf("% 5d ",jjcnt++);
- if (jjcnt>32675) jjcnt=0;
-
- while(inp(io320)!=88) {};
- STOP; /* halt */
- memcpy((char *)zh,adr1,1024);
- memcpy((char *)zl,adr2,1024);
- GO; /* go */
- for(k=0;k<512;k++) {
- z[k]=zh[k];
- z[k]=(z[k]<<16L)+zl[k];
- }
- }
- }
-
- cls()
- {
- printf("\x1B[2J");
- }
- /*
- cdecl sigpts(y,n)
- int *y,n;
- {
- int center;
- register int k;
- center = (640-n)/2;
- for(k=0;k<n;k++){
- _moveto(k+center,177);
- _setcolor(1);
- _lineto(k+center,*(y+k));
- _setcolor(0);
- _lineto(k+center,0);
- }
- }
- */
- ci()
- {
- int j;
- while (kbhit()==0) {};
- j=getch();
- }
-
- char scr_csts()
- {
- char c;
- if (kbhit()==0) return(0);
- if (getch()!=0) return(0);
- return(getch());
- }
-
- control_timer(cnt,osc,trate)
- long cnt;
- float *trate,osc;
- {
- int ctl,cth;
- /* tell board timer info coming */
- outp(cntrlpt ,CONTROL);
- /* count is timing divisor */
- ctl = cnt & 0xff; /* lo byte */
- cth = ( cnt >> 8 ) & 0xff;/* hi byte */
- /* control timer 0 */
- outp(prt0 , ctl);
- outp(prt0 , cth);
- /* control timer 2 */
- outp(cntrlpt , CONT2);
- outp(prt2 , ctl);
- outp(prt2 , cth);
- /* compute new sampling rate for output on screen*/
- *trate = osc/(float)cnt;
- }
- ref_spect(spect_count)
- int spect_count;
- {
- float t1,t2,t3,t4,norm_max;
- int binmax,jj,k;
-
- GO; /* go */
- for(k=0;k<512;k++) norm[k] = 0.0;
- for (jj=0;jj<spect_count;jj++) {
- _settextposition(4,1);printf("% 5d ",jj);
- while(inp(io320)!=88) {};
- STOP; /* halt */
- memcpy((char *)zh,adr1,1024);
- memcpy((char *)zl,adr2,1024);
- GO; /* go */
- for(k=0;k<512;k++) {
- z[k]=zh[k];
- z[k]=(z[k]<<16)+zl[k];
- }
- for(k=0;k<512;k++) norm[k] += z[k];
- }
- binmax=256;
- norm_max=-999.;
- t1 = t2 = t3 = t4 = 0.0;
- for (k=0;k<506;k++) { /* start with 7-point running average smoothing */
- t4=t3;
- t3=t2;
- t2=t1;
- t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
- norm[k]=t4/7.0; /* keep centered at proper index */
-
- if ( norm[k] > norm_max ) {
- binmax = k;
- norm_max = norm[k] ;
- }
- }
- for (k=0;k<25;k++) {
- norm[k]=0.;
- norm[511-k]=0.;
- }
- for (k=0;k<512;k++) {
- y[k]=180 - (norm[k]/norm_max)*100.;
- if ((norm[k]>100) && (k>60))
- norm[k] = norm_max/(norm[k]*512.0);
- else norm[k] = 0.;
- }
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- sigpts(&y[0],510);
- _settextposition(24,1);
- printf("Rate = %7.2f Hz freq=% 7.1f amp=% 8.1f ",
- trate, ((float)binmax)*trate/512.0,norm_max );
-
- }
-